//
//  BOTextField.m
//

#import "BOTextField.h"

@interface BOTextField()

@property (nonatomic, weak)UIImageView *searchIcon;

@property (nonatomic, assign)NSInteger maxChars;

@property (nonatomic ,copy) NSString *leftHighlightedImgName;
@property (nonatomic ,copy) NSString *leftnormalImgName;

@end

@implementation BOTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return self;
}

+ (id)setUpTextFieldFrame:(CGRect)frame Placeholder:(NSString *)placeholder textColor:(NSString *)textColor placeholderColor:(NSString *)placeholderColor textFont:(NSInteger)textFont leftImageName:(NSString *)leftImageName leftImageW:(CGFloat)leftImageW leftImageH:(CGFloat)leftImageH maxChars:(NSInteger)maxChars
{
    BOTextField *textField = [BOTextField new];
    
    // 通过init来创建初始化绝大部分控件，控件都是没有尺寸
    UIImageView *searchIcon = [[UIImageView alloc] init];
    textField.searchIcon = searchIcon;
    searchIcon.contentMode = UIViewContentModeCenter;
    textField.leftView = searchIcon;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftnormalImgName = leftImageName;
    
    
    textField.maxChars = maxChars;
    
    [textField setFrame:frame];
    
    textField.placeholder = placeholder;
    // 输入文字颜色
    textField.textColor = LGCOLOR(textColor);
    // 设置搜索框placeholder文字颜色
//    [textField setValue:LGCOLOR(placeholderColor) forKeyPath:@"_placeholderLabel.textColor"];
    textField.font = [UIFont systemFontOfSize:textFont];
    
    textField.searchIcon.image = [UIImage imageNamed:leftImageName];
    textField.searchIcon.width = leftImageW;
    textField.searchIcon.height = leftImageH;
    
    return textField;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self) {
        if (textField.text.length > self.maxChars) {
            textField.text = [textField.text substringToIndex:self.maxChars];
        }
        
        if (textField.text.length > 0 && self.leftHighlightedImgName) {
            self.searchIcon.image = [UIImage imageNamed:self.leftHighlightedImgName];
        }else{
            self.searchIcon.image = [UIImage imageNamed:self.leftnormalImgName];
        }
    }
    NSLog(@"%@", textField.text);
}

+ (id)setUpTextFieldFrame:(CGRect)frame Placeholder:(NSString *)placeholder textColor:(NSString *)textColor placeholderColor:(NSString *)placeholderColor textFont:(NSInteger)textFont leftImageName:(NSString *)leftImageName leftHighlightedImgName:(NSString *)leftHighlightedImgName leftImageW:(CGFloat)leftImageW leftImageH:(CGFloat)leftImageH maxChars:(NSInteger)maxChars
{
    BOTextField *textField = [BOTextField new];
    
    // 通过init来创建初始化绝大部分控件，控件都是没有尺寸
    UIImageView *searchIcon = [[UIImageView alloc] init];
    textField.searchIcon = searchIcon;
    searchIcon.contentMode = UIViewContentModeCenter;
    textField.leftView = searchIcon;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    textField.maxChars = maxChars;
    
    textField.leftHighlightedImgName = leftHighlightedImgName;
    
    textField.leftnormalImgName = leftImageName;
    
    [textField setFrame:frame];
    
    textField.placeholder = placeholder;
    // 输入文字颜色
    textField.textColor = LGCOLOR(textColor);
    // 设置搜索框placeholder文字颜色
//    [textField setValue:LGCOLOR(placeholderColor) forKeyPath:@"_placeholderLabel.textColor"];
    textField.font = [UIFont systemFontOfSize:textFont];
    
    textField.searchIcon.image = [UIImage imageNamed:leftImageName];
    textField.searchIcon.width = leftImageW;
    textField.searchIcon.height = leftImageH;
    
    return textField;
}

+ (id)setUpTextFieldFrame:(CGRect)frame Placeholder:(NSString *)placeholder textColor:(NSString *)textColor placeholderColor:(NSString *)placeholderColor textFont:(NSInteger)textFont leftTitle:(NSString *)leftTitle leftTitleW:(CGFloat)leftTitleW leftImageH:(CGFloat)leftImageH maxChars:(NSInteger)maxChars
{
    BOTextField *textField = [BOTextField new];
    
    // 通过init来创建初始化绝大部分控件，控件都是没有尺寸
    UIImageView *searchIcon = [[UIImageView alloc] init];
    textField.searchIcon = searchIcon;
    searchIcon.contentMode = UIViewContentModeCenter;
    textField.leftView = searchIcon;
    textField.leftViewMode = UITextFieldViewModeAlways;

    textField.maxChars = maxChars;
    
    [textField setFrame:frame];
    
    textField.placeholder = placeholder;
    // 输入文字颜色
    textField.textColor = LGCOLOR(textColor);
    textField.placeholder = leftTitle;
    // 设置搜索框placeholder文字颜色
//    [textField setValue:LGCOLOR(placeholderColor) forKeyPath:@"_placeholderLabel.textColor"];
    textField.font = [UIFont systemFontOfSize:textFont];
    
    textField.searchIcon.image = [UIImage imageNamed:@""];
    textField.searchIcon.width = 15;
    textField.searchIcon.height = leftImageH;
    
    return textField;
}


@end
